﻿#include <global_config.h>
#include <Wire.h>
#include <Arduino.h>
#include <Adafruit_SSD1327.h>
#include <hal_oled_ssd1327.h>

HAL_OLED_SSD1327 halOLEDSSD1327;

//定义 OLED屏幕的分辨率
Adafruit_SSD1327 oledDisplay(128, 128, OLED_MOSI_PIN, OLED_SCK_PIN, OLED_DC_PIN, OLED_RST_PIN, OLED_CS_PIN);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };


void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  uint8_t icons[NUMFLAKES][3];
  randomSeed(666);     // whatever seed
 
  // initialize
  for (uint8_t f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS] = random(oledDisplay.width());
    icons[f][YPOS] = 0;
    icons[f][DELTAY] = random(5) + 1;
    
    Serial.print("x: ");
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(" y: ");
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(" dy: ");
    Serial.println(icons[f][DELTAY], DEC);
  }

  while (1) {
    // draw each icon
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      oledDisplay.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, f+1);
    }
    oledDisplay.display();
    delay(200);
    
    // then erase it + move it
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      oledDisplay.drawBitmap(icons[f][XPOS], icons[f][YPOS],  bitmap, w, h, SSD1327_BLACK);
      // move it
      icons[f][YPOS] += icons[f][DELTAY];
      // if its gone, reinit
      if (icons[f][YPOS] > oledDisplay.height()) {
      	icons[f][XPOS] = random(oledDisplay.width());
      	icons[f][YPOS] = 0;
      	icons[f][DELTAY] = random(5) + 1;
      }
    }
   }
}


void testdrawchar(void) {
  oledDisplay.setTextSize(1);
  oledDisplay.setTextWrap(false);
  oledDisplay.setTextColor(SSD1327_WHITE);
  oledDisplay.setCursor(0,0);

  for (uint8_t i=0; i < 168; i++) {
    if (i == '\n') continue;
    oledDisplay.write(i);
    if ((i > 0) && (i % 21 == 0))
      oledDisplay.println();
  }    
  oledDisplay.display();
}

void testdrawcircle(void) {
  for (uint8_t i=0; i<oledDisplay.height(); i+=2) {
    oledDisplay.drawCircle(oledDisplay.width()/2, oledDisplay.height()/2, i, i % 15 + 1);
    oledDisplay.display();
  }
}

void testfillrect(void) {
  uint8_t color = 1;
  for (uint8_t i=0; i<oledDisplay.height()/2; i+=3) {
    // alternate colors
    oledDisplay.fillRect(i, i, oledDisplay.width()-i*2, oledDisplay.height()-i*2,  i % 15 + 1);
    oledDisplay.display();
    color++;
  }
}

void testdrawtriangle(void) {
  for (uint16_t i=0; i<min(oledDisplay.width(),oledDisplay.height())/2; i+=5) {
    oledDisplay.drawTriangle(oledDisplay.width()/2, oledDisplay.height()/2-i,
                     oledDisplay.width()/2-i, oledDisplay.height()/2+i,
                     oledDisplay.width()/2+i, oledDisplay.height()/2+i,  i % 15 + 1);
    oledDisplay.display();
  }
}

void testfilltriangle(void) {
  // uint8_t color = SSD1327_WHITE;
  for (int16_t i=min(oledDisplay.width(),oledDisplay.height())/2; i>0; i-=5) {
    oledDisplay.fillTriangle(oledDisplay.width()/2, oledDisplay.height()/2-i,
                     oledDisplay.width()/2-i, oledDisplay.height()/2+i,
                     oledDisplay.width()/2+i, oledDisplay.height()/2+i,  i % 15 + 1);
    oledDisplay.display();
  }
}

void testdrawroundrect(void) {
  for (uint8_t i=0; i<oledDisplay.height()/3-2; i+=2) {
    oledDisplay.drawRoundRect(i, i, oledDisplay.width()-2*i, oledDisplay.height()-2*i, oledDisplay.height()/4,  i % 15 + 1);
    oledDisplay.display();
  }
}

void testfillroundrect(void) {
  // uint8_t color = SSD1327_WHITE;
  for (uint8_t i=0; i<oledDisplay.height()/3-2; i+=2) {
    oledDisplay.fillRoundRect(i, i, oledDisplay.width()-2*i, oledDisplay.height()-2*i, oledDisplay.height()/4,  i % 15 + 1);
    oledDisplay.display();
  }
}
   
void testdrawrect(void) {
  for (uint8_t i=0; i<oledDisplay.height()/2; i+=2) {
    oledDisplay.drawRect(i, i, oledDisplay.width()-2*i, oledDisplay.height()-2*i, i % 15 + 1);
    oledDisplay.display();
  }
}

void testdrawline() {  
  for (uint8_t i=0; i<oledDisplay.width(); i+=4) {
    oledDisplay.drawLine(0, 0, i, oledDisplay.height()-1, SSD1327_WHITE);
    oledDisplay.display();
  }
  for (uint8_t i=0; i<oledDisplay.height(); i+=4) {
    oledDisplay.drawLine(0, 0, oledDisplay.width()-1, i, SSD1327_WHITE);
    oledDisplay.display();
  }
  delay(250);
  
  oledDisplay.clearDisplay();
  for (uint8_t i=0; i<oledDisplay.width(); i+=4) {
    oledDisplay.drawLine(0, oledDisplay.height()-1, i, 0, SSD1327_WHITE);
    oledDisplay.display();
  }
  for (int8_t i=oledDisplay.height()-1; i>=0; i-=4) {
    oledDisplay.drawLine(0, oledDisplay.height()-1, oledDisplay.width()-1, i, SSD1327_WHITE);
    oledDisplay.display();
  }
  delay(250);
  
  oledDisplay.clearDisplay();
  for (int8_t i=oledDisplay.width()-1; i>=0; i-=4) {
    oledDisplay.drawLine(oledDisplay.width()-1, oledDisplay.height()-1, i, 0, SSD1327_WHITE);
    oledDisplay.display();
  }
  for (int8_t i=oledDisplay.height()-1; i>=0; i-=4) {
    oledDisplay.drawLine(oledDisplay.width()-1, oledDisplay.height()-1, 0, i, SSD1327_WHITE);
    oledDisplay.display();
  }
  delay(250);

  oledDisplay.clearDisplay();
  for (uint8_t i=0; i<oledDisplay.height(); i+=4) {
    oledDisplay.drawLine(oledDisplay.width()-1, 0, 0, i, SSD1327_WHITE);
    oledDisplay.display();
  }
  for (uint8_t i=0; i<oledDisplay.width(); i+=4) {
    oledDisplay.drawLine(oledDisplay.width()-1, 0, i, oledDisplay.height()-1, SSD1327_WHITE); 
    oledDisplay.display();
  }
  delay(250);
}


void HAL_OLED_SSD1327::init()
{    
   
    Serial.println("OLED FeatherWing test"); // 串口输出
    while (! Serial) delay(100);
    Serial.println("SSD1327 OLED test");
    
    if ( !oledDisplay.begin(0x3D) ) {
      Serial.println("Unable to initialize OLED");
      while (1) yield();
    }

    Serial.println("111");
    oledDisplay.clearDisplay();

    Serial.println("2222");
    oledDisplay.display();

    Serial.println("3333");
    // draw many lines
    testdrawline();
    oledDisplay.display();
    delay(5000);
    oledDisplay.clearDisplay();

    // draw rectangles
    testdrawrect();
    oledDisplay.display();
    delay(5000);
    oledDisplay.clearDisplay();

    // draw multiple rectangles
    testfillrect();
    oledDisplay.display();
    delay(5000);
    oledDisplay.clearDisplay();

    // draw mulitple circles
    testdrawcircle();
    oledDisplay.display();
    delay(5000);
    oledDisplay.clearDisplay();

    // draw a SSD1327_WHITE circle, 10 pixel radius
    oledDisplay.fillCircle(oledDisplay.width()/2, oledDisplay.height()/2, 10, SSD1327_WHITE);
    oledDisplay.display();
    delay(5000);
    oledDisplay.clearDisplay();

    testdrawroundrect();
    delay(5000);
    oledDisplay.clearDisplay();

    testfillroundrect();
    delay(5000);
    oledDisplay.clearDisplay();

    testdrawtriangle();
    delay(5000);
    oledDisplay.clearDisplay();
    
    testfilltriangle();
    delay(5000);
    oledDisplay.clearDisplay();

    // draw the first ~12 characters in the font
    testdrawchar();
    oledDisplay.display();
    delay(5000);
    oledDisplay.clearDisplay();

    for (uint8_t rot=0; rot < 4; rot++) {
      oledDisplay.setRotation(rot);
      oledDisplay.clearDisplay();
      // text display tests
      oledDisplay.setTextSize(1);
      oledDisplay.setTextColor(SSD1327_WHITE);
      oledDisplay.setCursor(0,0);
      oledDisplay.println("Hello, world!");
      oledDisplay.setTextColor(SSD1327_BLACK, SSD1327_WHITE); // 'inverted' text
      oledDisplay.println(3.141592);
      oledDisplay.setTextSize(2);
      oledDisplay.setTextColor(SSD1327_WHITE);
      oledDisplay.print("0x"); oledDisplay.println(0xDEADBEEF, HEX);
      oledDisplay.display();
      delay(1000);
    }

    oledDisplay.setRotation(0);
    
    // miniature bitmap display
    oledDisplay.clearDisplay();
    oledDisplay.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
    oledDisplay.display();

    // invert the display
    oledDisplay.invertDisplay(true);
    delay(1000); 
    oledDisplay.invertDisplay(false);
    delay(1000); 

    // draw a bitmap icon and 'animate' movement
    testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
}

void HAL_OLED_SSD1327::updateDemo()
{
   
}
